﻿using System;
using System.Diagnostics;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKCodeBase.WinForm;

namespace FamilyCompare
{
	[Transaction(TransactionMode.Manual)]
	internal class FamilyCompareCommand : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIApplication application = commandData.Application;
			Application application2 = application.Application;
			UIDocument activeUIDocument = application.ActiveUIDocument;
			Document doc = null;
			try
			{
				doc = activeUIDocument.Document;
			}
			catch
			{
			}
			if (FamilyCompareCommand._hWndRevit == null)
			{
				Process currentProcess = Process.GetCurrentProcess();
				IntPtr mainWindowHandle = currentProcess.MainWindowHandle;
				FamilyCompareCommand._hWndRevit = new WindowHandle(mainWindowHandle);
			}
			if (FamilyCompareCommand.m_Form != null && !FamilyCompareCommand.m_Form.IsDisposed)
			{
				FamilyCompareCommand.m_Form.Close();
			}
			FamilyCompareCommand.m_Form = new FrmFamilyCompare(doc, application, FamilyCompareCommand._hWndRevit);
			FamilyCompareCommand.m_Form.Show(FamilyCompareCommand._hWndRevit);
			try
			{
				for (;;)
				{
					activeUIDocument.Selection.PickPoint(".");
				}
			}
			catch (Exception)
			{
				FamilyCompareCommand.m_Form.Close();
			}
			FamilyCompareCommand.m_Form.Close();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private static FrmFamilyCompare m_Form;

		public static WindowHandle _hWndRevit;
	}
}
